package com.example.saveword.save_word;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.saveword.R;
import com.example.saveword.dao.WordDao;
import com.example.saveword.model.Word;

import java.util.ArrayList;
import java.util.List;

public class WordListActivity extends AppCompatActivity implements View.OnClickListener{
    private Context context;
    private TextView tv_book_title;
    private List<Word> words = new ArrayList<>();
    private String wordListType;

    private WordDao wordDao;

    private ImageView btn_backToWordBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_list);
        init();
        getData();
        if(!wordListType.equals("my"))
        draw();
        else drawMy();
    }

    void init() {
        context = this;
        wordDao = new WordDao(this);
        tv_book_title = findViewById(R.id.tv_book_title);
        btn_backToWordBook = findViewById(R.id.btn_backToWordBook);
        btn_backToWordBook.setOnClickListener(this);
    }

    void getData() {
        // 接收来自上个界面的数据
        Bundle bundle = getIntent().getExtras();
        wordListType = bundle.getString("wordListType");
        if(wordListType.equals("CET4")) {
            tv_book_title.setText("四级词汇大全");
        } else if(wordListType.equals("CET6")) {
            tv_book_title.setText("六级词汇大全");
        } else if(wordListType.equals("my")) {
            tv_book_title.setText("我的单词本");
        }
        // 获取某类型的所有单词
        words = wordDao.getTypeWords(wordListType);
    }

    void draw() {
        // 绘制单词列表
        for(int i = 0; i < words.size(); i ++) {
            LayoutInflater inflater = LayoutInflater.from(WordListActivity.this);
            LinearLayout linearLayout = findViewById(R.id.layout_word_list);
            LinearLayout wordList = linearLayout.findViewById(R.id.ll_word_list);
            LinearLayout singleWord = inflater.inflate(R.layout.single_word_layout, null).findViewById(R.id.layout_single_word);
            TextView tv_list_word = singleWord.findViewById(R.id.tv_list_word);
            tv_list_word.setText(words.get(i).getHeadWord());
            int nowId = words.get(i).get_id();
            wordList.addView(singleWord);
            singleWord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setClass(context, WordDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("wordId", nowId);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }
    }
    void drawMy() {
        // 绘制单词列表
        words = wordDao.getTypeWords(wordListType);
        for(int i = 0; i < words.size(); i ++) {
            LayoutInflater inflater = LayoutInflater.from(WordListActivity.this);
            LinearLayout linearLayout = findViewById(R.id.layout_word_list);
            LinearLayout wordList = linearLayout.findViewById(R.id.ll_word_list);
            LinearLayout mySingleWord = inflater.inflate(R.layout.single_my_word_layout, null).findViewById(R.id.layout_single_word);
            TextView tv_list_word = mySingleWord.findViewById(R.id.tv_list_my_word);
            tv_list_word.setText(words.get(i).getHeadWord());
            int nowId = words.get(i).get_id();
            wordList.addView(mySingleWord);
            Button bt_update = mySingleWord.findViewById(R.id.modifyWord);
            Button bt_del = mySingleWord.findViewById(R.id.delWord);
            bt_update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showUpdateDialog(nowId);
                }
            });
            bt_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDelDialog(nowId);
                }
            });
            mySingleWord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setClass(context, WordDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("wordId", nowId);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }
    }
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_backToWordBook) {
            finish();
        }
    }
    protected void showUpdateDialog(Integer id) {
        LayoutInflater factory = LayoutInflater.from(context);
        final View textEntryView = factory.inflate(R.layout.updateword, null);
        final EditText update_word = (EditText) textEntryView
                .findViewById(R.id.update_word);
        final EditText update_mean = (EditText) textEntryView
                .findViewById(R.id.update_mean);
        final EditText update_voice = (EditText) textEntryView
                .findViewById(R.id.update_voice);
        final EditText update_exp = (EditText) textEntryView
                .findViewById(R.id.update_exp);
        final EditText update_En_mean = (EditText) textEntryView
                .findViewById(R.id.update_En_mean);
        final EditText update_WordGroup = (EditText) textEntryView
                .findViewById(R.id.update_WordGroup);
        final EditText update_near = (EditText) textEntryView
                .findViewById(R.id.update_near);
        AlertDialog.Builder ad1 = new AlertDialog.Builder(context);
        ad1.setTitle("修改单词信息:");
        ad1.setIcon(R.drawable.ic___);
        ad1.setView(textEntryView);
        ad1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                Word word = new Word();
                word.set_id(id);
                word.setHeadWord(update_word.getText().toString());
                word.setTranCN(update_mean.getText().toString());
                word.setSentences(update_voice.getText().toString());
                word.setUkphone(update_exp.getText().toString());
                word.setTranEN(update_En_mean.getText().toString());
                word.setPhrases(update_WordGroup.getText().toString());
                word.setSyno(update_near.getText().toString());
                word.setWordType("my");
                wordDao.update(word);
                LinearLayout linearLayout = findViewById(R.id.layout_word_list);
                LinearLayout wordList = linearLayout.findViewById(R.id.ll_word_list);
                wordList.removeAllViews();
                drawMy();
            }
        });
        ad1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {

            }
        });
        ad1.show();// 显示对话框
    }
    protected void showDelDialog(Integer id) {

        AlertDialog.Builder ad1 = new AlertDialog.Builder(context);
        ad1.setTitle("确定删除单词？");
        ad1.setIcon(R.drawable.ic___);
        ad1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                Word word = new Word();
                word.set_id(id);
                wordDao.delete(word);
                LinearLayout linearLayout = findViewById(R.id.layout_word_list);
                LinearLayout wordList = linearLayout.findViewById(R.id.ll_word_list);
                wordList.removeAllViews();
                drawMy();
            }
        });
        ad1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
            }
        });
        ad1.show();// 显示对话框
    }
}